#import "OmMomentsVCCommentViewSingleView.h"
#import "NoveltyApiNoveltyListModel.h"
@interface OmMomentsVCCommentViewSingleView ()
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UILabel *timeLabel;
@end
@implementation OmMomentsVCCommentViewSingleView
- (void)setModel:(OmCommentModel *)model {
    _model = model;
    self.nameLabel.text = model.uname;
    if ([model.parent_id isEqualToString:@"0"]) {
        self.contentLabel.text = model.content;
    } else {
        self.contentLabel.attributedText = [[NSMutableAttributedString alloc] init]
        .fp_appendAttributedString(ASLocalizedString(@"回复"), nil, nil)
        .fp_appendAttributedString([NSString stringWithFormat:@"%@: ", model.to_uname], nil, HexadecimalColor(@"EB99B9"))
        .fp_appendAttributedString(model.content, nil, nil);
    }
    self.timeLabel.text = [OmHandyMethods dateStringFromTimeStamp:model.create_time];
    UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(eventTapToReply)];
    [self addGestureRecognizer:recognizer];
}
- (void)eventTapToReply {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:ASLocalizedString(@"评论") message:nil preferredStyle:UIAlertControllerStyleAlert];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = [NSString stringWithFormat:@"%@ %@:", ASLocalizedString(@"回复"), self.model.uname];
    }];
    [alertController addAction:[UIAlertAction actionWithTitle:ASLocalizedString(@"发布") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"novelty_api" A:@"publish_comment"];
        NSMutableDictionary *param = [NSMutableDictionary dictionary];
        param[@"object_id"] = self.model.object_id;
        param[@"parent_id"] = self.model.ID;
        param[@"content"] = alertController.textFields[0].text;
        [CYNET POST:url parameters:param responseCache:^(id responseObject) {
        } success:^(id responseObject) {
            NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"novelty_api" A:@"novelty_list"];
            NSMutableDictionary *param = [NSMutableDictionary dictionary];
            param[@"uid"] = curUser.id;
            param[@"token"] = curUser.token;
            param[@"page"] = @(self.superview.superview.sd_tableView.refreshPage);
            [CYNET POST:url parameters:param responseCache:^(id responseObject) {
            } success:^(id responseObject) {
                NSMutableArray *modelArray = [NoveltyApiNoveltyListModel mj_objectArrayWithKeyValuesArray:responseObject];
                for (NoveltyApiNoveltyListModel *model in modelArray) {
                    if ([model.ID isEqualToString:self.model.object_id]) {
                        NSMutableArray *newArray = [self.superview.superview.sd_tableView.refreshDataArray mutableCopy];
                        [newArray replaceObjectAtIndex:self.superview.superview.sd_indexPath.row withObject:model];
                        self.superview.superview.sd_tableView.refreshDataArray = newArray;
                        [self.superview.superview.sd_tableView beginUpdates];
                        [self.superview.superview.sd_tableView reloadRowAtIndexPath:self.superview.superview.sd_indexPath withRowAnimation:UITableViewRowAnimationNone];
                        [self.superview.superview.sd_tableView endUpdates];
                    }
                }
            } failure:^(NSString *error) {
            } hasCache:NO];
        } failure:^(NSString *error) {
            if ([error isEqualToString:@"请先开通会员"]) {
                UIAlertController *controller = [UIAlertController alertControllerWithTitle:ASLocalizedString(@"请升级皇冠会员") message:nil preferredStyle:UIAlertControllerStyleAlert];
                [controller addAction:[UIAlertAction actionWithTitle:ASLocalizedString(@"确认") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    OmBuyKingHatVC *vc = [[OmBuyKingHatVC alloc] initWithType:MemberTypeKingHat];
                    [self.viewController.navigationController pushViewController:vc animated:YES];
                }]];
                [self.viewController presentViewController:controller animated:YES completion:nil];
            }
        } hasCache:NO];
    }]];
    [self.viewController presentViewController:alertController animated:YES completion:nil];
}
- (void)allocateComponents {
    self.nameLabel = [[UILabel alloc] init].fp_textColor(HexadecimalColor(@"#EB99B9")).fp_font([UIFont systemFontOfSize:16 weight:UIFontWeightMedium]);
    [self addSubview:self.nameLabel];
    self.contentLabel = [[UILabel alloc] init].fp_textColor(HexadecimalColor(@"#666666")).fp_font([UIFont systemFontOfSize:14 weight:UIFontWeightMedium]).fp_numberOfLines(0);
    [self addSubview:self.contentLabel];
    self.timeLabel = [[UILabel alloc] init].fp_textColor(HexadecimalColor(@"#A1A2A4")).fp_font([UIFont systemFontOfSize:12 weight:UIFontWeightMedium]);
    [self addSubview:self.timeLabel];
}
- (void)layoutComponents {
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self);
    }];
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLabel.mas_bottom).offset(5);
        make.left.right.equalTo(self);
        make.bottom.equalTo(self);
    }];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.equalTo(self);
    }];
}
@end
